package route;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author meluo
 * @version 2016/2/3
 */
public abstract class AbsFunction {
    public List<AbsFilter> filterList;
    public HttpServletRequest request;
    public ServletResponse response;

    public List<AbsFilter> getFilterList() {
        return filterList;
    }

    public void setFilterList(List<AbsFilter> filterList) {
        this.filterList = filterList;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public ServletResponse getResponse() {
        return response;
    }

    public void setResponse(ServletResponse response) {
        this.response = response;
    }
    
    
    /**添加拦截器的方法，建议在这里面添加参数验证**/
    public void addFilter(AbsFilter filter){
        if(filterList==null){
            filterList=new ArrayList();
        }
        filterList.add(filter);
        
    }
    public void addFilter(List<AbsFilter> filters){
        if(filterList==null){
            filterList=new ArrayList();
        }
        filterList.addAll(filters); 
    }
    public void beforeExcecute(){};
    /**执行方法体 所有操作在这里面进行
     * @return 返回参数列表会根据对应的参数跳转到相应的界面
     */
    public abstract String execute();
    
    /**
     * 
     * @return 
     */
    String exe() throws ServletException, IOException{
        if(filterList!=null&&!filterList.isEmpty()){
            for(AbsFilter filter:filterList){
                if(!filter.filter()){
                      request.setAttribute("errinfo",filter.getErrInfo());
                      RequestDispatcher rd = request.getRequestDispatcher(filter.getRetpage());
                      System.out.println("errinfo"+filter.getErrInfo()+"====page"+filter.getRetpage());
	              rd.forward(request, response);
                }
            }
                
        }
        return execute();
    }
    
}
